<?php
  require '../include/library.inc.php';
  require '../include/admin_login.inc.php';
    
  $school_id = intval($_GET['school_id']);

  if($school_id) {
	  
	$sql = "SELECT
			  order_id
			FROM
			  orders
			WHERE
			  school_id = ".$school_id." 
              ";
	$row = mysql_fetch_assoc(query($sql));
	
	if (!$row['order_id']) { // If now orders with this course	  
		
		$sql = "SELECT
				  order_number
				FROM
				  schools
				WHERE
				  school_id = ".$school_id." 
				  ";
		$row = mysql_fetch_assoc(query($sql));
		$order_number = $row['order_number'];
			
		$sql = "UPDATE
				  schools
				SET
				  order_number = order_number - 1
				WHERE
				  order_number > $order_number
				  ";
		query($sql);
			
		$sql = "SELECT
				  course_id
				FROM
				  courses
				WHERE
				  school_id = ".$school_id." 
				  ";
		query($sql);
		$result = query($sql);
	
		while ($row = mysql_fetch_object($result)) {	
			
			$sql = "DELETE FROM 
					   course_dates
					WHERE
					   course_id = ".$row->course_id." 
					 ";
			query($sql);
			
		}
		
		$sql = "DELETE FROM 
				   schools
				WHERE
				   school_id = ".$school_id." 
				 ";
		query($sql);
		
		$sql = "DELETE FROM 
				   courses
				WHERE
				   school_id = ".$school_id." 
				 ";
		query($sql);
		
		$sql = "DELETE FROM 
				   images
				WHERE
				   school_id = ".$school_id." 
				 ";
		query($sql);
			
		$_SESSION['message'] = 'The school was deleted!';
	
	} else {
		
		$_SESSION['message'] = 'There is orders with this school!';
		
	}
	
  }
  
  redirect("schools.php");
 
?>